package com.timk.goserver.server.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 * Hibernate utilities
 * @author TKington
 *
 */
public class HibernateUtil {
	private static final SessionFactory sessionFactory;
	
	static {
		try {
			sessionFactory = new Configuration().configure().buildSessionFactory();
		}
		catch(Exception e) {
			throw new ExceptionInInitializerError(e);
		}
	}
	
	/**
	 * Returns the SessionFactory
	 * @return the Session Factory
	 */
	public static SessionFactory getSessionFactory() { return sessionFactory; }
	
	/**
	 * Gets the current session and returns it after starting a transaction
	 * @return the session
	 */
	public static Session startTrans() {
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		return session;
	}
}
